import querystring from 'querystring';
import http from 'http'
import config from "../config/config";
export const sleep =(times:number)=> {
    let now = new Date();
    const exit = now.getTime() + times;
    while (true) {
        now = new Date();
        if (now.getTime() > exit) {
            return;
        }
    }
}

export  const sendPostHttpRequest =(body,path='/')=> {
    return new Promise((resolve, reject) => {
        let options = {
            hostname: config.server.host,
            port: config.server.port,
            path: path,
            method: 'POST',
            headers: {"content-type": 'application/json'}
        };

        var contents = JSON.stringify(body);

        var req = http.request(options, function(res){
            res.setEncoding('utf8');
            console.log('STATUS: ' + res.statusCode);
        });
        req.write(contents);
        req.end();
        req.on('error', function(e) {
            console.log('problem with request: ' + e.message);
        });
        resolve('执行成功');
    })
};